home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / appsrcs.zip / MORSETUP.ZIP / APPBROWS.C < prev    next >
C/C++ Source or Header  |  1992-08-11  |  8KB  |  266 lines

  1. #define STRICT
  2. #include <windows.h>
  3. #include <windowsx.h>
  4. #include <commdlg.h>
  5. #include <cderr.h>
  6. #include <string.h>
  7. #include <io.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include "morsetup.h"
  11.  
  12. // !!!!! Don't forget to link commdlg.dll !!!!!
  13.  
  14. BOOL PASCAL BrowseFile(char *FileName, HWND hWndCommDlgParent, BROWSEFILES FileType)
  15.     {
  16.     OPENFILENAME ofn;
  17.     char szDirName[256];
  18.     char szFile[256], szFileTitle[256]; /* file and title arrays */
  19.     char *szWaveFilter[] = {
  20.     "Sound Files (*.wav)",
  21.     "*.wav",
  22.     ""
  23.     };
  24. char *szIcoFilter[] = {
  25.     "Icon Resource Files",
  26.     "*.ico;*.exe;*.dll",
  27.     "All Files",
  28.     "*.*",
  29.     ""
  30.     };
  31. char *szExeFilter[] = {
  32.     "Executable Files",
  33.     "*.exe;*.com;*.pif",
  34.     "All Files"
  35.     "*.*",
  36.     ""
  37.     };
  38.  
  39.     /* Initialize the OPENFILENAME members */
  40.     GetWindowsDirectory((LPSTR)szDirName, 255);
  41.  
  42.     szFile[0] = '\0';
  43.     ofn.lStructSize = sizeof(OPENFILENAME);
  44.     ofn.hwndOwner = hWndCommDlgParent;
  45.     if(FileType == ICONFILES)
  46.     {
  47.     ofn.lpstrFilter = szIcoFilter[0];
  48.     ofn.lpstrTitle = (LPSTR) "AppBar Setup - Browse IconFiles";
  49.     ofn.lpstrInitialDir = AppSystem.DefaultIconDir;
  50.     }
  51.     if(FileType == EXEFILES)
  52.     {
  53.     ofn.lpstrFilter = szExeFilter[0];
  54.     ofn.lpstrTitle = (LPSTR) "AppBar Setup - Browse Executable Files";
  55.     ofn.lpstrInitialDir = szDirName;
  56.     }
  57.     if(FileType == WAVEFILES)
  58.     {
  59.     ofn.lpstrFilter = szWaveFilter[0];
  60.     ofn.lpstrTitle = (LPSTR) "AppBar Setup - Browse SoundFiles";
  61.     ofn.lpstrInitialDir = AppSound.SoundDirectory;
  62.     }
  63.     ofn.lpstrCustomFilter = (LPSTR) NULL;
  64.     ofn.nMaxCustFilter = 0L;
  65.     ofn.nFilterIndex = 1L;
  66.     ofn.lpstrFile= szFile;
  67.     ofn.nMaxFile = sizeof(szFile);
  68.     ofn.lpstrFileTitle = szFileTitle;
  69.     ofn.nMaxFileTitle = sizeof(szFileTitle);
  70.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  71.     ofn.nFileOffset = 0;
  72.     ofn.nFileExtension = 0;
  73.     ofn.lpstrDefExt = (LPSTR) NULL;
  74.  
  75.     /* Call the GetOpenFilename function */
  76.     if(GetOpenFileName(&ofn))
  77.     {
  78.     strcpy(FileName, (char *) ofn.lpstrFile);
  79.     return TRUE;
  80.     }
  81.     else
  82.     {
  83.     FileErrorHandler();
  84.     return FALSE;
  85.     }
  86.     }
  87.  
  88. /*--------------------------------------------------------------------------*/
  89. /* FUNCTION: SelectDirDlgProc()                         */
  90. /*                                        */
  91. /* PURPOSE:  Processes Select Directory Dialog Box Messages            */
  92. /*--------------------------------------------------------------------------*/
  93. BOOL WINAPI SelectDirDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  94.     {
  95.     static BOOL ListBoxFocus = FALSE;
  96.  
  97.     switch(message)
  98.         {
  99.     case WM_INITDIALOG:
  100.             strcpy(TempStr, Directory);
  101.             UpdateListBox(hDlg);
  102.         SetFocus(GetDlgItem(hDlg, IDDIR_LIST));
  103.             return FALSE;
  104.         break;
  105.  
  106.     case WM_COMMAND:
  107.             switch(wParam)
  108.                 {
  109.         case IDDIR_LIST:
  110.                     switch(HIWORD(lParam))
  111.                         {
  112.                         case LBN_SETFOCUS:
  113.                             ListBoxFocus = TRUE;
  114.                 SendDlgItemMessage(hDlg, IDDIR_LIST, LB_SETCURSEL,
  115.                               GetListBoxIndex(hDlg), 0L);
  116.                             return TRUE;
  117.  
  118.                         case LBN_KILLFOCUS:
  119.                             ListBoxFocus = FALSE;
  120.                 SendDlgItemMessage(hDlg, IDDIR_LIST, LB_SETCURSEL,
  121.                               -1, 0L);
  122.                             return TRUE;
  123.  
  124.                         case LBN_DBLCLK:
  125. DoubleClick:            DlgDirSelect(hDlg, TempStr, IDDIR_LIST);
  126.                             UpdateListBox(hDlg);
  127.                             return (TRUE);
  128.                             break;
  129.  
  130.             } /* end IDDIR_LIST switch */
  131.                     break;
  132.  
  133.         case IDDIR_OK:
  134.                     if(ListBoxFocus)
  135.                         goto DoubleClick;
  136.  
  137.             GetDlgItemText(hDlg, IDDIR_PATH, Directory, sizeof(Directory));
  138.             bNewDir = TRUE;
  139.             EndDialog(hDlg,TRUE);
  140.             return (TRUE);
  141.                     break;
  142.  
  143.         case IDDIR_CANCEL:
  144.             EndDialog(hDlg,FALSE);
  145.             bNewDir = FALSE;
  146.                     return (TRUE);
  147.                     break;
  148.  
  149.                 default:
  150.                     return (FALSE);
  151.                 } /* end WM_COMMAND SWITCH */
  152.         default:
  153.             return (FALSE);
  154.         }
  155.     }
  156.  
  157. /*--------------------------------------------------------------------------*/
  158. /* FUNCTION: UpdateListBox(HWND hDlg)                        */
  159. /*                                        */
  160. /* PURPOSE:  Helper function for dialog window proc                */
  161. /*--------------------------------------------------------------------------*/
  162. void UpdateListBox(HWND hDlg)
  163.     {
  164.     SetDlgItemText(hDlg, IDDIR_PATH, TempStr);
  165.     DlgDirList(hDlg, TempStr, IDDIR_LIST, IDDIR_PATH, DDL_DIRECTORY | DDL_DRIVES | DDL_EXCLUSIVE);
  166.     SendDlgItemMessage(hDlg, IDDIR_LIST, LB_SETCURSEL,
  167.       GetListBoxIndex(hDlg), 0L);
  168.     InvalidateRect(hDlg, NULL, TRUE);
  169.     UpdateWindow(hDlg);
  170.     }
  171.  
  172. /*--------------------------------------------------------------------------*/
  173. /* FUNCTION: GetListBoxIndex(HWND hDlg)                     */
  174. /*                                        */
  175. /* PURPOSE:  Helper function for dialog window proc                */
  176. /*--------------------------------------------------------------------------*/
  177. UINT GetListBoxIndex(HWND hDlg)
  178.     {
  179.     WORD rtn;
  180.  
  181.     rtn = (UINT) SendDlgItemMessage(hDlg, IDDIR_LIST, LB_GETCURSEL, 0, 0L);
  182.     if(rtn == LB_ERR)
  183.         return 0;
  184.     else
  185.         return rtn;
  186.     }
  187.  
  188. /*--------------------------------------------------------------------------*/
  189. void FileErrorHandler(void)
  190.     {
  191.     BOOL bDisplayError = TRUE;
  192.  
  193.     switch(CommDlgExtendedError())
  194.     {
  195.     case CDERR_FINDRESFAILURE:
  196.         strcpy(szBuffer,"Common dialog box function failed to\n find specified resource.");
  197.         break;
  198.  
  199.     case CDERR_INITIALIZATION:
  200.         strcpy(szBuffer,"Common dialog box function\n failed during initialization.");
  201.         break;
  202.  
  203.     case CDERR_LOCKRESFAILURE:
  204.         strcpy(szBuffer,"Common dialog box function failed to\nlock a specified resource.");
  205.         break;
  206.  
  207.     case CDERR_LOADRESFAILURE:
  208.         strcpy(szBuffer,"Common dialog box function failed to\nload a specified resource.");
  209.         break;
  210.  
  211.     case CDERR_LOADSTRFAILURE:
  212.         strcpy(szBuffer,"Common dialog box function failed to\nload a specified string.");
  213.         break;
  214.  
  215.     case CDERR_MEMALLOCFAILURE:
  216.         strcpy(szBuffer,"Common dialog box function was unable to\nallocate memory for internal datastructures.");
  217.         break;
  218.  
  219.     case CDERR_MEMLOCKFAILURE:
  220.         strcpy(szBuffer,"Common dialog box function was unable to\nlock the memory associated with a handle.");
  221.         break;
  222.  
  223.     case CDERR_NOHINSTANCE:
  224.         strcpy(szBuffer,"ENABLETEMPLATE flag was set in the Flags member\nof the common dialog box but\nthat the application failed to provide a corresponding instance handle.");
  225.         break;
  226.  
  227.     case CDERR_NOHOOK:
  228.         strcpy(szBuffer,"ENABLEHOOK flag was set in the Flags member\nof the common dialog box but that\nthe application failed to provide a pointer to a corresponding hook function.");
  229.         break;
  230.  
  231.     case CDERR_NOTEMPLATE:
  232.         strcpy(szBuffer,"ENABLETEMPLATE flag was set in the Flags member\nof the common dialog box but that\nthe application failed to provide a corresponding template.");
  233.         break;
  234.  
  235.     case CDERR_REGISTERMSGFAIL:
  236.         strcpy(szBuffer, "RegisterWindowMessage function returned\nan error value forthe common dialog box function.");
  237.         break;
  238.  
  239.     case CDERR_STRUCTSIZE:
  240.         strcpy(szBuffer, "Invalid lStructSize of the common dialog box.");
  241.         break;
  242.  
  243.     case FNERR_BUFFERTOOSMALL:
  244.         strcpy(szBuffer, "Buffer to which the lpstrFile of\nthe common dialog box function points is too small.");
  245.         break;
  246.  
  247.     case FNERR_INVALIDFILENAME:
  248.         strcpy(szBuffer, "Invalid filename.");
  249.         break;
  250.  
  251.     case FNERR_SUBCLASSFAILURE:
  252.         strcpy(szBuffer, "An attempt to subclass a list box\nfailed due to insufficient memory.");
  253.         break;
  254.  
  255.     case FRERR_BUFFERLENGTHZERO:
  256.         strcpy(szBuffer, "A member in a data structure for\nthe common dialog box points to an invalid buffer.");
  257.         break;
  258.  
  259.     default:
  260.         bDisplayError = FALSE;
  261.         break;
  262.     }
  263.     if(bDisplayError)
  264.     OkMsgBox("AppSetup - Browse", "Common dialog error:\n%s", szBuffer);
  265.     }
  266.